<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/6/7 0007
 * Time: 11:30
 */

namespace XtwsBack\Controller;


use Think\Controller;

class EvaluateController extends Controller
{
    public function index(){
        $type = $_GET['type']?$_GET['type']:'foretaste';
        if($type == 'foretaste')$table = 'foretaste_evaluate';//活动
        if($type == 'spellgroup')$table = 'evaluate';//拼团
        if($type == 'comment')$table = 'evaluate_comment';//回复
        $evaluate=M($table);
        $count = $evaluate->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $evaluate_arr =$evaluate->order('addtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('table',$table);// 赋值表名
        $this->assign('list',$evaluate_arr);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }//end fun

    public function delete(){
        $id  = intval(I('id'));//评论id
        $table = $_POST['table'];//数据表名
        if($id){
            if ($table != 'evaluate_comment'){
                $evaluate = M($table)->where("id = {$id}")->find();//评论信息
                $comment_id = M("evaluate_comment")->where("e_id = {$evaluate['id']} AND accept_id = {$evaluate['u_id']}")->field('id')->select();//获取该评论的所有回复id
                foreach($comment_id as $k => $v){
                    M("evaluate_comment")->where("id = {$v['id']}")->delete();//删除回复
                }//end foreach
            }//end if
            $res = M($table)->where("id = {$id}")->delete();//删除评论
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('出错啦，请检查');
            }//end if
        }else{
            $this->error('请先选择数据');
        }//end if
    }//end fun

}//end fun